package com.huatai.exchange.bootup.task;

import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.DateFormatUtils;
import com.faneui.common.utils.ListUtils;
import com.faneui.integration.http.HttpClientUtils;
import com.faneui.service.Env;
import com.huatai.exchange.dal.model.ExcAutoSendCustMailTask;
import com.huatai.exchange.dal.model.ExcExchangeInfo;
import com.huatai.exchange.facade.Contents;
import com.huatai.exchange.integration.base.service.WorkDayService;
import com.huatai.exchange.integration.support.service.ScheduleBatchService;
import com.huatai.exchange.service.ExcAutoSendCustMailTaskService;
import com.huatai.support.facade.bean.schedule.RegisterScheduleBatchReq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 自动每天创建人物批次
 *
 * @author fanrui16
 */
@Component
public class AutoCreateScheduleBatchTask {

    private static final Logger logger = LoggerFactory.getLogger(AutoCreateScheduleBatchTask.class);

    @Autowired
    private ScheduleBatchService scheduleBatchService;

    @Autowired
    private ExcAutoSendCustMailTaskService sendCustMailTaskService;

    @Autowired
    private WorkDayService workDayService;

    @Autowired
    private Env env;

    @Value("${server.port}")
    private String businSystemCode;

    /**
     * 根据任务信息派发任务  间隔两秒钟
     */
    @Scheduled(fixedDelay = 3000)
    public void run() {
        if (env.isLocal() || env.isDev()) {
            return;
        }
        String date = DateFormatUtils.yyyyMMdd.format(new Date());
        logger.info("date:" + date);
        try {
            ExcAutoSendCustMailTask param = new ExcAutoSendCustMailTask();
            param.setStatus(Contents.boolean_true);
            param.setNextDate(date);
            List<ExcAutoSendCustMailTask> tasks = sendCustMailTaskService.queryExcAutoSendCustMailTaskList(param);
            if (ListUtils.isEmpty(tasks)) {
                logger.info("当天无任务");
                return;
            }
            for (ExcAutoSendCustMailTask task : tasks) {
                String startTime = task.getStartTime();
                if (Integer.valueOf(DateFormatUtils.HHmmss.format(new Date())) < Integer.valueOf(startTime)) {
                    logger.info("任务【{}】未到时间", task.getName());
                    continue;
                }
                // 优化创建时间
                // TODO

                String nextDate = workDayService.getWorkDayNextWorkDay(date);
                Long num = sendCustMailTaskService.modifyNextDate(task.getId(), date, nextDate);
                if (num == 0) {
                    logger.info("任务【{}】已被创建", task.getName());
                    continue;
                }
                logger.info("任务【{}】注册开始", task.getName());
                RegisterScheduleBatchReq req = new RegisterScheduleBatchReq();

                req.setBusinId(task.getId());
                req.setTaskName(task.getName());
                req.setBusinSystemCode(businSystemCode);

                req.setSystemPort(businSystemCode);

                req.setHttpMethod("GET");
                req.setInterfaceUrl("/exchange/execute/"+task.getExchangeInfoId());

                scheduleBatchService.registerScheduleBatch(req);
                logger.info("任务【{}】注册成功", task.getName());

            }

        } catch (Exception ex) {
            logger.error("", ex);
        }
        logger.info("批次创建任务结束");
    }

}
